package ctrl.action.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Model;
import model.beans.CategoryBean;

import util.ProjectConstants;

import ctrl.action.ServletAction;

public class CatalogServletAction implements ServletAction{
	
	ServletContext context;	
	public CatalogServletAction(ServletContext context) {
		this.context = context;
	}
	
	@Override
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String jsp; 
		
		// get the model instance
		Model model = (Model)context.getAttribute(ProjectConstants.PARAMMODEL);	
		
		HttpSession session = request.getSession(true);
		
		// retrieve the list of CategoryBean from db source
		List<CategoryBean> categoryList = (List<CategoryBean>)model.getCategoryList();
		
		// set categoryList to the session scope for left navigation
		session.setAttribute(ProjectConstants.PARAMCATEGORYLIST, categoryList);
		
		//For returning page after login
		session.setAttribute(ProjectConstants.PARAMPREVIOUSPAGE, ProjectConstants.PARAMHOME);
		
		jsp = ProjectConstants.PAGEHOME;
		context.getRequestDispatcher("/" + jsp + ".jspx").forward(request, response);		
	}

}
